import { rendalist } from '../api/rendal';
const IndexModel = {
  namespace: 'rendal',

  state: {
    rendalist: [],
  },

  effects: {
    *GET_READEL({ payload }, { call, put }) {
      const list = yield rendalist({ payload });
      // console.log(list,'ata')
      yield put({ type: 'SET_READEL', payload: list.data.data });
    },
  },
  reducers: {
    SET_READEL(state, { payload }) {
      // console.log(state)
      state.rendalist = payload;
      return {
        ...state,
      };
    },
    ADD_READEL(state, { payload }) {
      const { title, id } = payload;
      // console.log(payload)
      const newState = JSON.parse(JSON.stringify(state));
      // console.log(newState)
      const index = newState.rendalist.findIndex((item) => {
        return item.id === id;
      });
      // console.log(index)
      newState.rendalist[index].title = title;
      // console.log(newState)
      return {
        ...newState,
      };
    },
    DEL(state, { payload }) {
      const newSatate = JSON.parse(JSON.stringify(state));
      newSatate.rendalist.splice(payload, 1);
      return {
        ...newSatate,
      };
    },
    GET_SEARCH(state, { payload }) {
      // console.log(payload)
      const searcharr = state.rendalist.filter((item) => {
        return item.title.includes(payload);
      });
      return {
        ...state,
        rendalist: searcharr,
      };
    },
    // 启用 immer 之后
    // save(state, action) {
    //   state.name = action.payload;
    // },
  },
  subscriptions: {
    AAA({ dispatch }) {
      dispatch({ type: 'GET_READEL' });
    },
  },
};

export default IndexModel;
